/**
 * @author stuart adam lamour
 */
(function($){
    // JQUERY
    $(document).ready(function(){
        // TBD - variables.			
        var $skilltag = $('ul#skillcloud li a.skill'); // the skill tags
        // EACH BEHAVIOUR IS WRITEN AS A SEPERATE INDIVIDUAL FUNCTION BLOCK
        // TO REMOVE THE BEHAVIOUR, JUST REMOVE THE FUNCTION BLOCK
        
        
        
        // CLICK ON A SKILL AND PROVIDE THE SKILL INFORMATION
        $($skilltag).click(function(event){
            var skill = $(this).text();
            
            //position variables 
            var top = event.pageY + 18;
            var left = event.pageX;
            var boxWidth = $('div#skill_information').width();
            // check box will not go outside the window
            if ($(window).width() < boxWidth + left) {
                left = left - boxWidth;
            }
            
            // if the box is not positioned already
            $('div#skill_information:not(.positioned)').css({
                "position": "absolute",
                "left": left,
                "top": top
            });
            
            $('div#skill_information').fadeIn('slow');
            $('div#skill_information:visible').animate({
                "opacity": 0.75
            }, 100, function callback(){
                $(this).animate({
                    "opacity": 1
                }, 500);
            });
            
            // put it in the dom
            $(this).after($('div#skill_information'));
            
            $.get('skillclouds-includes/sc-fatcontroller.php', {
                skill: skill,
                information: 'definition'
            }, function(definition){
                // alert (definition);
                $('div#skill_information_content p.definition').html(definition);
            });
            
            
            
            $.get('skillclouds-includes/sc-fatcontroller.php', {
                skill: skill,
                information: 'encompass'
            }, function(encompass){
                $('div#skill_information_content p.encompass').html(encompass);
            });
            
            
            $.get('skillclouds-includes/sc-fatcontroller.php', {
                skill: skill,
                information: 'demonstrate'
            }, function(demonstrate){
                $('div#skill_information_content p.demonstrate').html(demonstrate);
            });
            
            
            $('div#skill_information_content h2.skill_title').text($(this).text());
            
        });
        
        
        
        // SKILL INFOMATION DRAG AND DROP FUNCTIONALITY
        // SAFARI does not like containment 'html' so we are using 'body' 
        // but this means firefox makes the dragable area really small and can produce odd effects...
        $('div#skill_information').draggable({
            opacity: 0.5,
            cursor: 'move',
            containment: 'body',
            handle: 'div#skill_information_content h2',
            start: function(event, ui){
                $(this).addClass('positioned');
            }
        });
        
        
        
        
        // CONTROLS FOR SKILL INFORMATION FUNCTIONALITY - CLOSE, ENLARGE, REDUCE
        $('span#skill_information_display_options a').click(function(){
            var display_request = $(this).text();
            if (display_request.match('close')) {
                // close the element, reset the position, and remove positioned class
                $('div#skill_information').fadeOut('slow', function callback(){
                    $(this).css({
                        left: "",
                        top: "",
                        width: "",
                        height: ""
                    });
                    $(this).removeClass('positioned');
                });
                $('span#skill_information_display_options a:contains("reduce")').addClass('sc_hide');
                $('span#skill_information_display_options a:contains("expand")').removeClass('sc_hide');
            }
            else {
                $('span#skill_information_display_options a.sc_hide').removeClass('sc_hide');
                $(this).addClass('sc_hide');
                if (display_request.match('expand')) {
                    $('div#skill_information').animate({
                        "left": "2em",
                        "width": "90%"
                    }, "slow").addClass('positioned');
                }
                if (display_request.match('reduce')) {
                    $('div#skill_information').animate({
                        "width": "40em"
                    }, "slow");
                }
            }
            
        });
        
        
        
        // CONTROLLS FOR LIST AND CLOUD VIEW FUNCTIONALITY - VIEW AS LIST, VIEW AS CLOUD  
        $('span#tagcloud_display_options a').click(function(){
            var display_request = $(this).text();
            $('span#tagcloud_display_options a.sc_hide').removeClass('sc_hide');
            $(this).addClass('sc_hide');
            if (display_request.match('list')) {
                $('ul#skillcloud').fadeOut('', function(){
                    $(this).removeClass('cloud').addClass('list')
                });
                $('ul#skillcloud').fadeIn();
            }
            else {
                $('ul#skillcloud').fadeOut('', function(){
                    $(this).removeClass('list').addClass('cloud')
                });
                $('ul#skillcloud').fadeIn();
            }
        });
        
        
        
        // GENERIC HIDE AND SHOW FUNCTIONALITY		
        $('#skillclouds_content_div .sc_show').toggle(function(){
            $(this).next().show('fast');
        }, function(){
            $(this).next().hide('fast');
        });
        
        
        
        
        // A TO Z LIST HIGHLIGHTING FUNCTIONALITY - SKILLS BEGINING WITH THE LETTER CLICKED ARE HIGHLIGHTED
        $('#skillclouds_tagcloud_menu a.skillclouds_atoz_menu').click(function(){
            var letter = $(this).text();
            var $skills = $('ul#skillcloud li a')
            
            $.each($skills, function(){
                var text = $(this).text();
                if (text.charAt(0) == letter.charAt(0)) {
                    $(this).addClass('sc_highlight');
                }
                else {
                    $(this).removeClass('sc_highlight');
                }
            });
        });
        
        
        
        // SEARCH HIGHLIGHTING FUNCTIONALITY - SKILLS CONTAINING THE SEARCH TERM ARE HIGHLIGHTED
        $("form#skillclouds_search").submit(function(){
            return false;
        });
        var searchterm = '';
        $('#skillclouds_search input').keyup(function(){
            searchterm = $(this).val();
            // $('#testterm').html(searchterm);			
            var $skills = $('ul#skillcloud li a.skill');
            $.each($skills, function(){
                var skill = $(this).text();
                if (skill.match(searchterm) && searchterm.length > 0) {
                    $(this).addClass('sc_highlight');
                    // TBD - highlight the text within the term searched for ??
                }
                else {
                    $(this).removeClass('sc_highlight');
                    
                }
            });
        });
        
    });
})(jQuery);


